package org.ielse.frame.test;

import java.util.ArrayList;
import java.util.List;

import org.ielse.frame.BaseActivity;
import org.ielse.frame.R;
import org.ielse.frame.t.view.recycler.DividerItemDecoration;
import org.ielse.frame.t.view.ultra.PtrDefaultHandler;
import org.ielse.frame.t.view.ultra.PtrFrameLayout;
import org.ielse.frame.t.view.ultra.PtrHandler;
import org.ielse.frame.t.view.ultra.header.MaterialHeader;
import org.ielse.frame.util.BaseUtil;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;
//import org.ielse.frame.t.view.recycler.paulburke.helper.SimpleItemTouchHelperCallback;
//import android.support.v7.widget.helper.ItemTouchHelper;

public class Recycler33333Activity extends BaseActivity {

	// PtrFrameLayout mPtrFrameLayout ;

	private RecyclerView recyclerView1;
	RecyclerAdapter1 adapter1;
	private LinearLayoutManager layoutManager;

	private final List<MData> valueList = new ArrayList<MData>();

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_recycler333);
		super.onCreate(savedInstanceState);

		// mPtrFrameLayout= (PtrFrameLayout)
		// findViewById(R.id.material_style_ptr_frame);
		// final MaterialHeader header = new MaterialHeader(context);
		// int[] colors = new int[] { 0xffff00cc, 0xffcc00ff, 0xff00ffcc };
		// header.setColorSchemeColors(colors);
		// header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
		// header.setPadding(0, BaseUtil.dipToPx(15), 0, BaseUtil.dipToPx(10));
		// header.setPtrFrameLayout(mPtrFrameLayout);
		//
		// mPtrFrameLayout.setDurationToCloseHeader(1500);
		// mPtrFrameLayout.setHeaderView(header);
		// mPtrFrameLayout.addPtrUIHandler(header);
		// mPtrFrameLayout.postDelayed(new Runnable() {
		// @Override public void run() {
		// mPtrFrameLayout.autoRefresh(false);
		// }
		// }, 400);
		//
		// mPtrFrameLayout.setPtrHandler(new PtrHandler() {
		// @Override public boolean checkCanDoRefresh(PtrFrameLayout frame, View
		// content, View header) {
		// return PtrDefaultHandler.checkContentCanBePulledDown(frame,
		// recyclerView1, header);
		// // return true;
		// }
		//
		// @Override public void onRefreshBegin(final PtrFrameLayout frame) {
		// frame.postDelayed(new Runnable() {
		// @Override public void run() {
		// frame.refreshComplete();
		// }
		// }, 1000);
		// }
		// });

		//

		for (int i = 0; i < 22; i++) {
			MData md = new MData();
			if (i % 4 == 0) {
				md.type = MData.TEXT;
			}
			else if (i % 4 == 1) {
				md.type = MData.VIDEO;
			}
			else if (i % 4 == 2) {
				md.type = MData.SHARE;
			}
			else {
				md.type = MData.PICTURE;
			}
			md.content = "item " + i;
			valueList.add(md);
		}

		layoutManager = new LinearLayoutManager(this);
		layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		recyclerView1 = (RecyclerView) findViewById(R.id.view_recycler1);
		recyclerView1.setHasFixedSize(true);
		recyclerView1.setLayoutManager(layoutManager);
		// 设置Item增加、移除动画
		recyclerView1.setItemAnimator(new DefaultItemAnimator());
		// 添加分割线
		recyclerView1.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
		recyclerView1.setAdapter(adapter1 = new RecyclerAdapter1(valueList));

	}

	public class MData {
		public static final byte TEXT = 1;
		public static final byte SHARE = 2;
		public static final byte PICTURE = 3;
		public static final byte VIDEO = 4;
		int type;
		String content;
		boolean select;
	}

	public static class RecyclerAdapter1 extends RecyclerView.Adapter<RecyclerAdapter1.BaseViewHolder> {

		private List<MData> mValueList;

		public RecyclerAdapter1(List<MData> valueList) {
			if (valueList == null) throw new IllegalArgumentException("The data can not be empty");
			mValueList = valueList;
		}

		public void add(MData item, int position) {
			mValueList.add(position, item);
			notifyItemInserted(position);
		}

		public void remove(MData item) {
			int position = mValueList.indexOf(item);
			mValueList.remove(position);
			notifyItemRemoved(position);
			notifyItemRangeChanged(position, getItemCount());
		}

		public interface OnRecyclerViewListener {
			void onItemClick(int position);
		}

		private OnRecyclerViewListener onRecyclerViewListener;

		public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
			this.onRecyclerViewListener = onRecyclerViewListener;
		}

		public class BaseViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
			public BaseViewHolder(View view) {
				super(view);
				view.setOnClickListener(this);
			}

			int position;

			@Override public void onClick(View v) {
				final MData md = mValueList.get(position);
				md.select = !md.select;

				v.setBackgroundColor(md.select ? 0X33000000 : 0Xffffffff);

				if (null != onRecyclerViewListener) onRecyclerViewListener.onItemClick(position);
			}
		}

		public class ViewHolder1 extends BaseViewHolder {
			public ViewHolder1(View view) {
				super(view);
			}

			android.widget.ImageView mImg;
			TextView mTxt3;
		}

		public class ViewHolder2 extends BaseViewHolder {
			public ViewHolder2(View view) {
				super(view);
			}

			android.widget.ImageView mImg;
			TextView mTxt4;
		}

		@Override public int getItemCount() {
			return mValueList.size();
		}

		@Override public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
			switch (getItemViewType(i)) {
			default:
			case MData.TEXT:
			case MData.SHARE:
				View view1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_recycler, viewGroup, false);
				ViewHolder1 viewHolder1 = new ViewHolder1(view1);
				viewHolder1.mImg = (android.widget.ImageView) view1.findViewById(R.id.img_anim1);
				viewHolder1.mTxt3 = (TextView) view1.findViewById(R.id.tx_test_3);
				return new ViewHolder1(view1);
			case MData.PICTURE:
			case MData.VIDEO:
				View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_recycler_222, viewGroup, false);
				ViewHolder2 viewHolder2 = new ViewHolder2(view2);
				viewHolder2.mImg = (android.widget.ImageView) view2.findViewById(R.id.img_anim2);
				viewHolder2.mTxt4 = (TextView) view2.findViewById(R.id.tx_test_4);
				return new ViewHolder1(view2);
			}
		}

		@Override public int getItemViewType(int position) {
			return mValueList.get(position).type;
		}

		@Override public void onBindViewHolder(final BaseViewHolder viewHolder, final int i) {
			final MData md = mValueList.get(i);
			switch (getItemViewType(i)) {
			default:
			case MData.TEXT:
			case MData.SHARE:
				ViewHolder1 holder1 = (ViewHolder1) viewHolder;
				holder1.mTxt3.setText(md.content);
				holder1.itemView.setBackgroundColor(md.select ? 0X33000000 : 0Xffffffff);
			case MData.PICTURE:
			case MData.VIDEO:
				ViewHolder2 holder2 = (ViewHolder2) viewHolder;
				holder2.mTxt4.setText(md.content);
				holder2.itemView.setBackgroundColor(md.select ? 0X33000000 : 0Xffffffff);
			}

			viewHolder.position = i;
		}
	}

}
